﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GMS.BasicControls
{
    public partial class UnitsList : BackGroundControl
    {
        public UnitsList()
        {
            InitializeComponent();
        }
        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        DataTable source = new DataTable();
        SqlDataAdapter adapter = new SqlDataAdapter();
        private void UnitsList_Load(object sender, EventArgs e)
        {
            source.RowDeleted += new DataRowChangeEventHandler(source_RowDeleted);
            source.TableNewRow += new DataTableNewRowEventHandler(source_TableNewRow);
            source.RowChanged += new DataRowChangeEventHandler(source_RowChanging);
            dataGridView1.DataSource = source;
            
            adapter.SelectCommand = new SqlCommand("DSelectUnit", new SqlConnection(ConnectionString));
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            adapter.Fill(source);

            adapter.InsertCommand = new SqlCommand("DInsertUnit", new SqlConnection(ConnectionString));
            adapter.InsertCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter insertParameter = null;
            insertParameter = adapter.InsertCommand.Parameters.Add("@DUnitCaption", SqlDbType.VarChar);
            insertParameter.SourceColumn = "UnitCaption";
            insertParameter.SourceVersion = DataRowVersion.Current;

            adapter.DeleteCommand = new SqlCommand("DDeleteUnit", new SqlConnection(ConnectionString));
            adapter.DeleteCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter deleteParameter = null;
            deleteParameter = adapter.DeleteCommand.Parameters.Add("@IdUnit", SqlDbType.Int);
            deleteParameter.SourceColumn = "IdUnit";
            deleteParameter.SourceVersion = DataRowVersion.Current;

            adapter.UpdateCommand = new SqlCommand("DUpdateUnit", new SqlConnection(ConnectionString));
            adapter.UpdateCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter updateParameter = null;
            updateParameter = adapter.UpdateCommand.Parameters.Add("@IdUnit", SqlDbType.Int);
            updateParameter.SourceColumn = "IdUnit";
            updateParameter.SourceVersion = DataRowVersion.Current;

            updateParameter = adapter.UpdateCommand.Parameters.Add("@UnitCaption", SqlDbType.VarChar);
            updateParameter.SourceColumn = "UnitCaption";
            updateParameter.SourceVersion = DataRowVersion.Current;
            
        }

        void source_RowChanging(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Change)
                adapter.Update(source);
        }

        void source_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            adapter.Update(source);
        }


        void source_RowDeleted(object sender, DataRowChangeEventArgs e)
        {
            adapter.Update(source);
        }

        private void applyLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }
    }
}
